$.fn.select0 = function (options) {
    var defaults = {
        change: $.noop,
        keyUp:$.noop
    };
    var settings = $.extend(defaults, options);
    $(this).each(function () {
        var o = $(this);
        var multiple = o.attr('multiple');
        var container = o.closest('div');
        /*-------重组SELECT标签-------*/
        var A = $('<a></a>');
        A.addClass('select-choice select-choice2');
        A.attr('tabindex', '0');
        var span = $('<span class="spanIuput" contenteditable="false" style="width:100%;height:100%;display:block;outline: none;"></span>');
        span.attr('data-target', '-2');
        var sOptions = o.find('option:selected');
        if (!sOptions || sOptions.length <= 0) {
            sOptions = o.find('option:first');
        }
        if (multiple) {
            var text = '';
            for (var index = 0; index < sOptions.length; index++) {
                text += $(sOptions[index]).text();
                if (index != sOptions.length - 1) {
                    text += '，';
                }
            }
            span.text(text);
        }
        else {
            span.text(sOptions.text());
        }
        var i = $('<i></i>');
        i.addClass('icon icon-xiala');
        A.append(span).append(i);
        var UL = $('<ul></ul>');
        UL.addClass('select-drop select-results');
        var options = o.find('option');
        for (var index = 0; index < options.length; index++) {
            var option = $(options[index]);
            var Li = $('<li></li>');
            Li.attr('data-idx', index);
            var selected = option.attr('selected');
            if (selected) {
                Li.addClass('select-highlighted');
            }
            if (multiple) {
                if (selected) {
                    Li.html('<input type="checkbox" checked>' + option.text());
                } else {
                    Li.html('<input type="checkbox">' + option.text());
                }
            } else {
                Li.text(option.text());
            }
            UL.append(Li);
        }
        var _span = $('<span></span>');
        _span.append(A).append(o).append(UL);
        container.empty();
        container.append(_span);
        /*-------重组SELECT标签-------*/

        /*-------绑定点击展开下拉事件-------*/
        container.find('a.select-choice').off("click").on('click', function (e) {
            $('.select-container.select').removeClass('select-container-active select-dropdown-open');
        	e.stopPropagation();
            if(span.text()=='-请选择-'){
            	span.text('');
            }
            if (container.hasClass('select-container-active select-dropdown-open')) {
                container.removeClass('select-container-active select-dropdown-open');
               // A.off('focus');
            } else {
                container.addClass('select-container-active select-dropdown-open');
                //A.trigger('focus');
            }
        	var bck = function(event){
        		if($(event.target).parents(".select-drop").length<1 && container.hasClass('select-container-active select-dropdown-open') && !$(event.target).is($(this))){
  				   container.removeClass('select-container-active select-dropdown-open');
              	}
        	}
        	container.on("keyup",".spanIuput",function(){
        		if(settings.keyUp){
        			settings.keyUp(this);
        		}
        	});
        	$("body").off("click",bck);
            $("body").on("click",bck);
            
        });
        /*-------绑定点击展开下拉事件-------*/

        container.find('ul li').on('mouseover', function () {
            span.data('target', $(this).data('idx'));
        });
        container.find('ul li').on('mouseout', function () {
            span.data('target', '-2');
        });

        container.off("click").on('click',".select-drop li", function () {
        	span.data('target', $(this).data('idx'));
            var idx = span.data('target');
            if (idx >= 0) {
                var sIdxOption = o.find('option:eq(' + idx + '):selected');
                //if (!sIdxOption || sIdxOption.length == 0) {
                    container.find('ul li').removeClass('select-highlighted');
                    container.find('ul li:eq(' + idx + ')').addClass('select-highlighted');
                    o.find('option').prop('selected', false);
                    o.find('option:eq(' + idx + ')').prop('selected', true);
                    span.text(container.find('ul li[data-idx='+idx+']').text());
                    //alert(container.find('ul li[data-idx='+idx+']').text())
                    o.trigger('change');
                //}
            }
            if (container.hasClass('select-container-active select-dropdown-open')) {
                container.removeClass('select-container-active select-dropdown-open');
            }
        });

    });

};

$.fn.cLearSelect0 = function () {
    $(this).each(function () {
        var o = $(this);
        o.val('');
        o.find('option:eq(0)').attr('selected', 'selected');
        o.closest('div').find('a.select-choice span').text(o.find('option:eq(0)').text());
    });
};


$.fn.select1 = function (options) {
    var defaults = {
        change: $.noop,
        keyUp:$.noop
    };
    var settings = $.extend(defaults, options);
    $(this).each(function () {
        var o = $(this);
        var multiple = o.attr('multiple');
        var container = o.closest('div');
        /*-------重组SELECT标签-------*/
        var A = $('<a></a>');
        A.addClass('select-container2-a');
        A.attr('tabindex', '0');

        var input = container.find('input');
        input.attr('data-target', '-2');
        input.css({'cursor':'pointer'});

        var sOptions = o.find('option:selected');
        if (!sOptions || sOptions.length <= 0) {
            sOptions = o.find('option:first');
        }
        if (multiple) {
            var text = '';
            for (var index = 0; index < sOptions.length; index++) {
                text += $(sOptions[index]).text();
                if (index != sOptions.length - 1) {
                    text += '，';
                }
            }
            input.val(text);
        } else {
            input.val(sOptions.text());
        }
        var i = $('<i></i>');
        i.addClass('icon1 icon1-jiantouxia1');
        A.append(i);
        var UL = $('<ul></ul>');
        UL.addClass('select-drop select-results');
        var options = o.find('option');
        for (var index = 0; index < options.length; index++) {
            var option = $(options[index]);
            var Li = $('<li></li>');
            Li.attr('data-idx', index);
            var selected = option.attr('selected');
            if (selected) {
                Li.addClass('select-highlighted');
            }
            if (multiple) {
                if (selected) {
                    Li.html('<input type="checkbox" checked>' + option.text());
                } else {
                    Li.html('<input type="checkbox">' + option.text());
                }
            } else {
                Li.text(option.text());
            }
            UL.append(Li);
        }
        var _span = $('<span></span>');
        _span.append(A).append(o).append(UL);
        //container.empty();
        container.find('span').remove();
        container.append(_span);
        /*-------重组SELECT标签-------*/

        /*-------绑定点击展开下拉事件-------*/
        container.find('input,a.select-container2-a').off("click").on('click', function (e) {
            $('.select-container.select-container2.select').removeClass('select-container-active select-dropdown-open');
        	e.stopPropagation();
            if(input.val() == '-请选择-') {
                input.val('');
            }

            if (container.hasClass('select-container-active select-dropdown-open')) {
                container.removeClass('select-container-active select-dropdown-open');
               // A.off('focus');
            } else {
                container.addClass('select-container-active select-dropdown-open');
                //A.trigger('focus');
            }
        	var bck = function(event){
        		if($(event.target).parents(".select-drop").length<1 && container.hasClass('select-container-active select-dropdown-open') && !$(event.target).is($(this))){
  				   container.removeClass('select-container-active select-dropdown-open');
              	}
        	};
        	container.on("keyup",".spanIuput",function(){
        		if(settings.keyUp){
        			settings.keyUp(this);
        		}
        	});
        	$("body").off("click",bck);
            $("body").on("click",bck);
            
        });
        /*-------绑定点击展开下拉事件-------*/

        container.find('ul li').on('mouseover', function () {
            input.data('target', $(this).data('idx'));
        });
        container.find('ul li').on('mouseout', function () {
            input.data('target', '-2');
        });

        container.off("click").on('click',".select-drop li", function () {
        	input.data('target', $(this).data('idx'));
            var idx = input.data('target');//span.data('target');
            if (idx >= 0) {
                //var sIdxOption = o.find('option:eq(' + idx + '):selected');
                //if (!sIdxOption || sIdxOption.length == 0) {
                    container.find('ul li').removeClass('select-highlighted');
                    container.find('ul li:eq(' + idx + ')').addClass('select-highlighted');
                    o.find('option').prop('selected', false);
                    o.find('option:eq(' + idx + ')').prop('selected', true);
                    var identifier = o.find('option:eq(' + idx + ')').attr('identifier');
                    if (identifier) {
                        input.data('identifier', identifier);
                    } else {
                        input.data('identifier', '');
                    }
                    input.val(container.find('ul li[data-idx='+idx+']').text());
                    o.trigger('change');
                //}
            }
            if (container.hasClass('select-container-active select-dropdown-open')) {
                container.removeClass('select-container-active select-dropdown-open');
            }
        });

    });

};

$.fn.cLearSelect1 = function () {
    $(this).each(function () {
        var o = $(this);
        o.val('');
        o.find('option:eq(0)').attr('selected', 'selected');
        //o.closest('div').find('a.select-choice span').text(o.find('option:eq(0)').text());
        o.closest('div').find('input').val(o.find('option:eq(0)').text());
    });
};


$.fn.chain = function (options) {
    var defaults = {
        defaultText: '--/--/--',
        separator: '/',
        callbackFun1: $.noop,
        callbackFun2: $.noop,
        callbackFun3: $.noop,
        callbackFun4: $.noop
    }
    var settings = $.extend(defaults, options);
    $(this).each(function () {
        var o = $(this);
        var container = o.parent('div');
        var A = $('<a></a>');
        A.addClass('select-choice');
        var ei = $('<i></i>');
        ei.addClass('icon icon-xiala');
        var UL = o.find('ul');
        var text = '';
        for (var i = 0; i < UL.length; i++) {
            var textLi = o.find('ul:eq(' + i + ') li.selected').text();
            if (textLi) {
                if (i != 0) {
                    text += settings.separator;
                }
                text += textLi;
            }
        }
        if ('' == text) {
            text = settings.defaultText;
        }
        var span = $('<span></span>');
        span.text(text);
        A.append(span).append(ei);
        var regionA = o.find('.region a');
        if (regionA.length > 4) {
            regionA.addClass('region-4-tab');
        } else {
            regionA.addClass('region-' + regionA.length + '-tab');
        }
        container.empty();
        container.append(A).append(o);

        A.on('click', function () {
            o.find('.region a').removeClass('active');
            o.find('.region a:eq(0)').addClass('active');
            o.find('ul').hide();
            o.find('ul:eq(0)').show();
            if (container.hasClass('select-dropdown-open')) {
                container.removeClass("select-dropdown-open");
            } else {
                container.addClass("select-dropdown-open");
            }
        });

        UL.each(function (index) {
            $(this).find('li').on('click', function () {
                var tempSpan = container.find('a.select-choice span');
                var tempText = tempSpan.text();
                if (index == 0) {
                    tempText = $(this).text();
                } else {
                    tempText += settings.separator + $(this).text();
                }
                tempSpan.text(tempText);
                if (index == 0) {
                    if (settings.callbackFun1) {
                        settings.callbackFun1(this);
                    }
                } else if (index == 1) {
                    if (settings.callbackFun2) {
                        settings.callbackFun2(this);
                    }
                } else if (index == 2) {
                    if (settings.callbackFun3) {
                        settings.callbackFun3(this);
                    }
                } else if (index == 3) {
                    if (settings.callbackFun4) {
                        settings.callbackFun4(this);
                    }
                }
                var regionIndex = o.find('.region a:eq(' + (index + 1) + ')');
                if (regionIndex.length > 0) {
                    o.find('.region a').removeClass('active');
                    regionIndex.addClass('active');
                }
                o.find('ul').hide();
                o.find('ul:eq(' + (index + 1) + ')').show();
                if (index == UL.length - 1) {
                    o.parent('div').removeClass('select-dropdown-open');
                }
            });
        });
    });
};

$.fn.extend({
    //---元素拖动插件
    Dragging: function (data) {
        var $this = $(this);
        var xPage;
        var yPage;
        var X;//
        var Y;//
        var xRand = 0;//
        var yRand = 0;//
        var father = $this.parent().parent();
        var defaults = {
            move: 'both',
            randomPosition: true,
            hander: 1
        }
        var opt = $.extend({}, defaults, data);
        var movePosition = opt.move;
        var random = opt.randomPosition;

        var hander = opt.handler;

        if (hander == 1) {
            hander = $this;
        } else {
            hander = $this.find(opt.handler);
        }


        //---初始化
        father.css({"position": "fixed", "overflow": "hidden"});
        $this.css({"position": "absolute"});
        hander.css({"cursor": "move"});

        var faWidth = father.width();
        var faHeight = father.height();
        var thisWidth = $this.width() + parseInt($this.css('padding-left')) + parseInt($this.css('padding-right'));
        var thisHeight = $this.height() + parseInt($this.css('padding-top')) + parseInt($this.css('padding-bottom'));

        var mDown = false;//
        var positionX;
        var positionY;
        var moveX;
        var moveY;

        if (random) {
            $thisRandom();
        }
        function $thisRandom() { //随机函数
            $this.each(function (index) {
                var randY = parseInt(Math.random() * (faHeight - thisHeight));///
                var randX = parseInt(Math.random() * (faWidth - thisWidth));///
                if (movePosition.toLowerCase() == 'x') {
                    $(this).css({
                        left: randX
                    });
                } else if (movePosition.toLowerCase() == 'y') {
                    $(this).css({
                        top: randY
                    });
                } else if (movePosition.toLowerCase() == 'both') {
                    $(this).css({
                        top: randY,
                        left: randX
                    });
                }

            });
        }

        hander.mousedown(function (e) {
            father.children().css({"zIndex": "0"});
            $this.css({"zIndex": "1"});
            mDown = true;
            X = e.pageX;
            Y = e.pageY;
            positionX = $this.position().left;
            positionY = $this.position().top;

            return false;
        });

        $(document).mouseup(function (e) {
            mDown = false;
        });

        $(document).mousemove(function (e) {
            xPage = e.pageX;//--
            moveX = positionX + xPage - X;

            yPage = e.pageY;//--
            moveY = positionY + yPage - Y;

            function thisXMove() { //x轴移动
                if (mDown == true) {
                    $this.css({"left": moveX});
                } else {
                    return;
                }
                if (moveX < 0) {
                    //$this.css({"left":"0"});
                }
                if (moveX > (faWidth - thisWidth)) {
                    //$this.css({"left":faWidth-thisWidth});
                }
                return moveX;
            }

            function thisYMove() { //y轴移动
                if (mDown == true) {
                    $this.css({"top": moveY});
                } else {
                    return;
                }
                if (moveY < 0) {
                    $this.css({"top": "0"});
                }
                if (moveY > (faHeight - thisHeight)) {
                    $this.css({"top": faHeight - thisHeight});
                }
                return moveY;
            }

            function thisAllMove() { //全部移动
                if (mDown == true) {
                    $this.css({"left": moveX, "top": moveY});
                } else {
                    return;
                }
                if (moveX < 0) {
                    //$this.css({"left":"0"});
                }
                if (moveX > (faWidth - thisWidth)) {
                    //$this.css({"left":faWidth-thisWidth});
                }

                if (moveY < 0) {
                    //$this.css({"top":"0"});
                }
                if (moveY > (faHeight - thisHeight)) {
                    //$this.css({"top":faHeight-thisHeight});
                }
            }

            if (movePosition.toLowerCase() == "x") {
                thisXMove();
            } else if (movePosition.toLowerCase() == "y") {
                thisYMove();
            } else if (movePosition.toLowerCase() == 'both') {
                thisAllMove();
            }
        });
    }
});